package org.fnzn.service.Impl;

import cn.hutool.core.date.DateUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.collections.ListUtils;
import org.apache.commons.lang3.StringUtils;
import org.fnzn.bean.*;
import org.fnzn.bean.fnxf.FnxfStaffReport;
import org.fnzn.bean.statisbean.StatisWhere;
import org.fnzn.bean.vo.User;
import org.fnzn.core.CommonConstant;
import org.fnzn.core.Constants;
import org.fnzn.core.Page;
import org.fnzn.core.Result;
import org.fnzn.dao.FnglCustomerMapper;
import org.fnzn.dao.FnglStaffMapper;
import org.fnzn.service.FnglCustomerService;
import org.fnzn.service.FnglStaffService;
import org.fnzn.service.base.AbstractService;
import org.fnzn.service.fnxf.FnxfStaffReportService;
import org.fnzn.util.CommonUtil;
import org.fnzn.util.DateTools;
import org.fnzn.util.ResultGenerator;
import org.fnzn.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Condition;
import tk.mybatis.mapper.entity.Example.Criteria;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class FnglCustomerServiceImpl extends AbstractService<FnglCustomer> implements FnglCustomerService {

    @Autowired(required = false)
    private FnglCustomerMapper fnglCustomerMapper;
    @Autowired
    private FnglStaffService fnglStaffService;
    @Autowired
    private FnxfStaffReportService fnxfStaffReportService;
    @Autowired
    private UserRoleServiceImpl userRoleServiceImpl;
    @Autowired(required = false)
    private FnglStaffMapper fnglStaffMapper;
    @Override
    public Result findFnglCustomer(FnglCustomer fnglCustomer, Page page) {
        if(page.getLimit()!=null&& page.getPage()!=0){
            List<FnglCustomer> list = new ArrayList<>();
            // 获取到当前登录用户所属角色
            User user = CommonUtil.getUser();
            LrdUserrole userrole = userRoleServiceImpl.findBy("userId",user.getUser_id());
            String roleId = userrole.getRoleId();
            // 根据用户手机号获取到人员信息
            FnglStaff fnglStaff = fnglStaffService.findBy("phone", user.getUser_id());

            // 如果当前是销售经理登录，那么只能看到自己录入的销售人员的客户资源池信息
            if(roleId.equals(Constants.MANAGER_ROLE)){
                Condition newcondition = new Condition(FnglStaff.class);
                Criteria newcriteria=newcondition.createCriteria();
                newcriteria.andEqualTo("delFlag",0);
                newcriteria.andEqualTo("blamer",fnglStaff.getStaffId());
                newcriteria.andEqualTo("roleId",Constants.SALESMAN_ROLE);
                List<FnglStaff> fnglStaffs = fnglStaffService.findByCondition(newcondition);
                // 新建集合存放销售人员id
                List<String> staffList = new ArrayList<>();
                if(fnglStaffs!=null&&fnglStaffs.size()>0){
                    for(FnglStaff item : fnglStaffs){
                        staffList.add(item.getStaffId());
                    }
                    PageHelper.startPage(page.getPage(),page.getLimit(), true);
                    list = fnglCustomerMapper.findFnglCustomer(fnglCustomer, staffList);
                }
            }
            // 如果当前是销售人员登录
            if(roleId.equals(Constants.SALESMAN_ROLE)){
                // 获取到当前登录用户手机号
                String phone = user.getUser_id();
                // 根据手机号获取到人员信息
                String staffId = fnglStaffMapper.getStaffId(phone);
                fnglCustomer.setStaffId(staffId);
                List<String> staffList = new ArrayList<>();
                PageHelper.startPage(page.getPage(),page.getLimit(), true);
                list = fnglCustomerMapper.findFnglCustomer(fnglCustomer,staffList);
            }
            // 如果当前是超级管理员登录
            if(roleId.equals(Constants.ADMIN_ROLE)){
                List<String> staffList = new ArrayList<>();
                PageHelper.startPage(page.getPage(),page.getLimit(), true);
                list = fnglCustomerMapper.findFnglCustomer(fnglCustomer,staffList);
            }
            page.setRows(list);
            page.setTotal(new PageInfo(list).getTotal());
            return ResultGenerator.genSuccessResult(page);
        }
        return ResultGenerator.genFailResult("查询失败");
    }

    private void initCriteria(Criteria criteria, FnglCustomer fnglCustomer) {
        // 根据删除状态查询
        criteria.andEqualTo("delFlag",0);
        Date date = new Date();//获取当前时间
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.SECOND, -15*24*3600);
        Date calendarTime = calendar.getTime();
        criteria.andGreaterThanOrEqualTo("createTime",calendarTime);
        // 获取到当前登录用户所属角色
        User user = CommonUtil.getUser();
        LrdUserrole userrole = userRoleServiceImpl.findBy("userId",user.getUser_id());
        String roleId = userrole.getRoleId();
        // 根据用户手机号获取到人员信息
        FnglStaff fnglStaff = fnglStaffService.findBy("phone", user.getUser_id());
        // 如果当前是销售经理登录，那么只能看到自己录入的销售人员的客户资源池信息
        if(roleId.equals(Constants.MANAGER_ROLE)){
            Condition newcondition = new Condition(FnglStaff.class);
            Criteria newcriteria=newcondition.createCriteria();
            newcriteria.andEqualTo("delFlag",0);
            newcriteria.andEqualTo("blamer",fnglStaff.getStaffId());
            newcriteria.andEqualTo("roleId",Constants.SALESMAN_ROLE);
            List<FnglStaff> fnglStaffs = fnglStaffService.findByCondition(newcondition);
            // 新建集合存放销售人员id
            List<String> list = new ArrayList<>();
            if(fnglStaffs!=null&&fnglStaffs.size()>0){
                for(FnglStaff item : fnglStaffs){
                    list.add(item.getStaffId());
                }
                criteria.andIn("staffId",list);
            }
        }
        // 如果当前是销售人员登录
        if(roleId.equals(Constants.SALESMAN_ROLE)){
            // 获取到当前登录用户手机号
            String phone = user.getUser_id();
            // 根据手机号获取到人员信息
            String staffId = fnglStaffMapper.getStaffId(phone);
            criteria.andEqualTo("staffId",staffId);
        }
        // 客户姓名
        if(!StringUtil.isEmpty(fnglCustomer.getCustomerName())){
            criteria.andLike("customerName","%"+fnglCustomer.getCustomerName()+"%");
        }
        // 项目id
        if(!StringUtil.isEmpty(fnglCustomer.getProjectId())){
            criteria.andEqualTo("projectId",fnglCustomer.getProjectId());
        }
        // 手机号码
        if(!StringUtil.isEmpty(fnglCustomer.getPhone())){
            criteria.andEqualTo("phone",fnglCustomer.getPhone());
        }
        // 录入日期
        if(!StringUtil.isEmpty(fnglCustomer.getStartTime())&&!StringUtil.isEmpty(fnglCustomer.getEndTime())){

            Date date1 = DateTools.parseDate(fnglCustomer.getStartTime());
            Date date2 = DateTools.parseDate(fnglCustomer.getEndTime());

            criteria.andBetween("createTime",date1,date2);
        }
        // 认识途径
        if(!StringUtil.isEmpty(fnglCustomer.getKnowWay())){
            criteria.andEqualTo("knowWay",fnglCustomer.getKnowWay());
        }
    }

    @Override
    public Result getFnglCommonCustomer(FnglCustomer fnglCustomer,Page page) {
        if(page.getLimit()!=null&& page.getPage()!=0){
            List<FnglCustomer> list = new ArrayList<>();
            // 获取到当前登录用户所属角色
            User user = CommonUtil.getUser();
            LrdUserrole userrole = userRoleServiceImpl.findBy("userId",user.getUser_id());
            String roleId = userrole.getRoleId();
            // 根据用户手机号获取到人员信息
            FnglStaff fnglStaff = fnglStaffService.findBy("phone", user.getUser_id());
            // 如果当前是销售经理登录，那么只能看到自己录入的销售人员的客户资源池信息
            if(roleId.equals(Constants.MANAGER_ROLE)){
                Condition newcondition = new Condition(FnglStaff.class);
                Criteria newcriteria=newcondition.createCriteria();
                newcriteria.andEqualTo("delFlag",0);
                newcriteria.andEqualTo("blamer",fnglStaff.getStaffId());
                newcriteria.andEqualTo("roleId",Constants.SALESMAN_ROLE);
                List<FnglStaff> fnglStaffs = fnglStaffService.findByCondition(newcondition);
                // 新建集合存放销售人员id
                List<String> staffList = new ArrayList<>();
                if(fnglStaffs!=null&&fnglStaffs.size()>0){
                    for(FnglStaff item : fnglStaffs){
                        staffList.add(item.getStaffId());
                    }
                    PageHelper.startPage(page.getPage(),page.getLimit(), true);
                    list = fnglCustomerMapper.getFnglCommonCustomer(fnglCustomer, staffList);
                }
            }
            // 如果当前是销售人员登录
            if(roleId.equals(Constants.SALESMAN_ROLE)){
            // 获取到当前登录用户手机号
            String phone = user.getUser_id();
            // 根据手机号获取到人员信息
            String staffId = fnglStaffMapper.getStaffId(phone);
            FnglStaff staffId1 =  fnglStaffService.findBy("staffId", staffId);
            Condition newcondition = new Condition(FnglStaff.class);
            Criteria newcriteria=newcondition.createCriteria();
            newcriteria.andEqualTo("delFlag",0);
            newcriteria.andEqualTo("blamer",staffId1.getBlamer());
            newcriteria.andEqualTo("roleId",Constants.SALESMAN_ROLE);
            List<FnglStaff> fnglStaffs = fnglStaffService.findByCondition(newcondition);
            // 新建集合存放销售人员id
            List<String> staffList = new ArrayList<>();
            if(fnglStaffs!=null&&fnglStaffs.size()>0){
                for(FnglStaff item : fnglStaffs){
                    staffList.add(item.getStaffId());
                }
                PageHelper.startPage(page.getPage(),page.getLimit(), true);
                list = fnglCustomerMapper.getFnglCommonCustomer(fnglCustomer, staffList);
            }
        }
            // 如果当前是超级管理员登录
            if(roleId.equals(Constants.ADMIN_ROLE)){
                List<String> staffList = new ArrayList<>();
                PageHelper.startPage(page.getPage(),page.getLimit(), true);
                list = fnglCustomerMapper.getFnglCommonCustomer(fnglCustomer,staffList);
            }
            page.setRows(list);
            page.setTotal(new PageInfo(list).getTotal());
            return ResultGenerator.genSuccessResult(page);
        }
        return ResultGenerator.genFailResult("查询失败");
    }
    @Override
    public Result insertFnglCustomer(FnglCustomer fnglCustomer) {

        if(fnglCustomer!=null){
            fnglCustomer.setCustomerId(StringUtil.getUUID());
        }else {
            return ResultGenerator.genFailResult("项目为空");
        }
        // 获取到当前登录用户
        User user = CommonUtil.getUser();
        String user_id = user.getUser_id();
        // 根据传来的登录用户手机号获取到销售人员id
        String staffId = fnglStaffService.getStaffId(user_id);
        fnglCustomer.setStaffId(staffId);
        // 录入客户时判断有没有报备过
        // 获取到报备表数据
        Condition condition1 = new Condition(FnxfStaffReport.class);
        Criteria criteria1=condition1.createCriteria();
        criteria1.andEqualTo("delFlag",0);
        List<FnxfStaffReport> fnxfStaffReportList = fnxfStaffReportService.findByCondition(condition1);
        if(fnxfStaffReportList!=null && fnxfStaffReportList.size()>0){
            for(FnxfStaffReport item : fnxfStaffReportList){
                if(item.getPhone().equals(fnglCustomer.getPhone())){
                    return ResultGenerator.genFailResult("该手机号已经报备过！");
                }
            }
        }
        // 录入客户时是否已经存在相同手机号码
            Condition condition = new Condition(FnglCustomer.class);
            Criteria criteria=condition.createCriteria();
            criteria.andEqualTo("delFlag",0);
            List<FnglCustomer> fnglCustomers = findByCondition(condition);

            if(fnglCustomers!=null&&fnglCustomers.size()>0){
                // 遍历集合
                for(FnglCustomer item : fnglCustomers){
                    if(item.getPhone().equals(fnglCustomer.getPhone())){
                        return ResultGenerator.genFailResult("已存在相同手机号，请重新录入！");
                    }
                }
            }
//        }
        int n = save(fnglCustomer);
        if(n>0){
            return ResultGenerator.genSuccessResult(fnglCustomer);
        }else{
            return ResultGenerator.genFailResult("添加失败");
        }
    }

    @Override
    public Result updateFnglCustomer(FnglCustomer fnglCustomer) {
        // 如果去填写二次回访，那么更新客户录入时间
        if(!StringUtil.isEmpty(fnglCustomer.getSecondDescription())){
            fnglCustomer.setCreateTime(new Date());
        }
        // 如果去填写三次回访，那么更新客户录入时间
        if(!StringUtil.isEmpty(fnglCustomer.getThirdDescription())){
            fnglCustomer.setCreateTime(new Date());
        }
        // 如果去填写四次回访，那么更新客户录入时间
        if(!StringUtil.isEmpty(fnglCustomer.getFourthDescription())){
            fnglCustomer.setCreateTime(new Date());
        }
        fnglCustomer.setUpdateTime(new Date());
        int n = update(fnglCustomer);
        if(n>0){
            return ResultGenerator.genSuccessResult(fnglCustomer);
        }else{
            return ResultGenerator.genFailResult("更新失败");
        }
    }

    @Override
    public Result transferFnglCustomer(FnglCustomer fnglCustomer,User user) {

        if(user == null){
            return ResultGenerator.genFailResult("移交失败");
        }
        // 判断是否已经被其他人移交
        FnglCustomer fnglCustomer1 = findById(fnglCustomer.getCustomerId());
        if(!fnglCustomer1.getStaffId().equals(fnglCustomer.getStaffId())){
            return ResultGenerator.genFailResult("已经抢先被其他人提交啦");
        }
        String phone = user.getUser_id();
        String staffId = fnglStaffService.getStaffId(phone);
        // 保存销售姓名
        FnglStaff fnglStaff = fnglStaffService.findBy("staffId", staffId);
        fnglCustomer.setStaffId(staffId);
        fnglCustomer.setCreateTime(new Date());
        int n = update(fnglCustomer);
        if(n>0){
            return ResultGenerator.genSuccessResult(fnglCustomer);
        }else{
            return ResultGenerator.genFailResult("移交失败");
        }
    }

    @Override
    public Result deleteFnglCustomer(String customerId) {

        boolean flag = fnglCustomerMapper.deleteFnglCustomer(customerId);
        if(flag){
            return ResultGenerator.genSuccessResult("删除成功");
        }else{
            return ResultGenerator.genFailResult("删除失败");
        }
    }

    @Override
    public Result deleteAllFnglCustomer(String Ids) {
        if(!StringUtil.isEmpty(Ids)){

            String[] CustomerArr = Ids.split(",");
            int n = 0;
            for(String item : CustomerArr){
                boolean flag = fnglCustomerMapper.deleteFnglCustomer(item);
                if(flag){
                    n+=1;
                }
            }
            if(n == CustomerArr.length){
                return ResultGenerator.genSuccessResult("删除成功");
            }else{
                return ResultGenerator.genFailResult("删除失败");
            }

        }else{
            return ResultGenerator.genFailResult("删除失败");
        }
    }

    /**
     *  根据客户手机号获取到客户姓名
     */
    @Override
    public Result getCustomer(String phone){

        if(StringUtil.isEmpty(phone)){
            return ResultGenerator.genFailResult("获取失败");
        }

        List<FnglCustomer> list = fnglCustomerMapper.getFnglCustomer(phone);
        return ResultGenerator.genSuccessResult(list);
    }

    /**
     *   根据销售人员id获取到其所有客户的客户电话
     */
    @Override
    public Result getCustomerPhone(String staffId){

        if(StringUtil.isEmpty(staffId)){
            return ResultGenerator.genFailResult("获取失败");
        }
        FnglCustomerBean fnglCustomer = new FnglCustomerBean();
        // 根据销售人员id获取到其所有的客户
        Condition condition = new Condition(FnglCustomer.class);
        Criteria criteria=condition.createCriteria();
        criteria.andEqualTo("delFlag",0);
        criteria.andEqualTo("staffId",staffId);
        List<FnglCustomer> fnglCustomers = findByCondition(condition);
        // 新建List
        List<FnglCustomerBean> list = new ArrayList<>();
        for(FnglCustomer item : fnglCustomers){

            if(!StringUtil.isEmpty(item.getCustomerId())){
                fnglCustomer = fnglCustomerMapper.getCustomerPhone(item.getCustomerId());
                list.add(fnglCustomer);
            }
        }
        return ResultGenerator.genSuccessResult(list);
    }

    /**
     * 获取到各个楼盘的客户详情
     */
    @Override
    public Result getProjectCustomer(Page page) {

        if (page.getLimit() != null && page.getPage() != 0) {
            // 获取到当前登录用户所属角色
            User user = CommonUtil.getUser();
            LrdUserrole userrole = userRoleServiceImpl.findBy("userId", user.getUser_id());
            String roleId = userrole.getRoleId();
            // 根据用户手机号获取到人员信息
            FnglStaff fnglStaff = fnglStaffService.findBy("phone", user.getUser_id());
            List<FnglCustomer> list = new ArrayList<>();
            PageHelper.startPage(page.getPage(), page.getLimit(), true);
            // 如果当前是销售经理登录，那么只能看到自己所属项目的客户详情
            if (roleId.equals(Constants.MANAGER_ROLE)) {
                list = fnglCustomerMapper.getProjectCustomer(fnglStaff.getProjectId());
            }
            // 如果当前是超级管理员登录，那么可以看到所有楼盘的客户详情
            if (roleId.equals(Constants.ADMIN_ROLE)) {
                list = fnglCustomerMapper.getProjectCustomer("");
            }

            page.setRows(list);
            page.setTotal(new PageInfo(list).getTotal());
            return ResultGenerator.genSuccessResult(page);
        }
        return ResultGenerator.genFailResult("查询失败");
    }

    /**
     * 获取到已成交客户年龄段所占比例
     */
    @Override
    public Result getCustomerAge(Page page){

            if (page.getLimit() != null && page.getPage() != 0) {

                // 获取到当前登录用户所属角色
                User user = CommonUtil.getUser();
                LrdUserrole userrole = userRoleServiceImpl.findBy("userId", user.getUser_id());
                String roleId = userrole.getRoleId();
                // 根据用户手机号获取到人员信息
                FnglStaff fnglStaff = fnglStaffService.findBy("phone", user.getUser_id());
                List<FnglCustomer> list = new ArrayList<>();
                PageHelper.startPage(page.getPage(), page.getLimit(), true);
                // 如果当前是销售经理登录，那么只能看到自己所属项目的已成交客户年龄段比例
                if (roleId.equals(Constants.MANAGER_ROLE)) {
                    list = fnglCustomerMapper.getCustomerAge(fnglStaff.getProjectId());
                }
                // 如果当前是超级管理员登录，那么可以看到所有项目的已成交客户年龄段比例
                if (roleId.equals(Constants.ADMIN_ROLE)) {
                    list = fnglCustomerMapper.getCustomerAge("");
                }
                page.setRows(list);
                page.setTotal(new PageInfo(list).getTotal());
                return ResultGenerator.genSuccessResult(page);
            }
            return ResultGenerator.genFailResult("查询失败");
        }

    @Override
    public Result getUnTurnOver(Page page){

        if (page.getLimit() != null && page.getPage() != 0) {

            // 获取到当前登录用户所属角色
            User user = CommonUtil.getUser();
            LrdUserrole userrole = userRoleServiceImpl.findBy("userId", user.getUser_id());
            String roleId = userrole.getRoleId();
            // 根据用户手机号获取到人员信息
            FnglStaff fnglStaff = fnglStaffService.findBy("phone", user.getUser_id());
            List<FnglCustomer> list = new ArrayList<>();
            PageHelper.startPage(page.getPage(), page.getLimit(), true);
            // 如果当前是销售经理登录，那么只能看到自己所属项目的已成交客户年龄段比例
            if (roleId.equals(Constants.MANAGER_ROLE)) {
                list = fnglCustomerMapper.getUnTurnOver(fnglStaff.getProjectId());
            }
            // 如果当前是超级管理员登录，那么可以看到所有项目的已成交客户年龄段比例
            if (roleId.equals(Constants.ADMIN_ROLE)) {
                list = fnglCustomerMapper.getUnTurnOver("");
            }

            page.setRows(list);
            page.setTotal(new PageInfo(list).getTotal());
            return ResultGenerator.genSuccessResult(page);
        }
        return ResultGenerator.genFailResult("查询失败");
    }

    /**
     * 已成交客户按照区域、职业、是否贷款统计情况
     */
    @Override
    public Result getTurnOverCustomer(Page page,StatisWhere statisWhere) {

        if (page.getLimit() != null && page.getPage() != 0) {

            // 获取到当前登录用户所属角色
            User user = CommonUtil.getUser();
            LrdUserrole userrole = userRoleServiceImpl.findBy("userId", user.getUser_id());
            String roleId = userrole.getRoleId();
            // 根据用户手机号获取到人员信息
            FnglStaff fnglStaff = fnglStaffService.findBy("phone", user.getUser_id());
            List<FnglCustomer> list = new ArrayList<>();
            PageHelper.startPage(page.getPage(), page.getLimit(), true);
            //初始化统计sql
            initStatistics(statisWhere);
            // 如果当前是销售经理登录，那么只能看到自己所属项目的已成交客户年龄段比例
            if (roleId.equals(Constants.MANAGER_ROLE)) {
                list = fnglCustomerMapper.getTurnOverCustomer(fnglStaff.getProjectId(), statisWhere);
            }
            // 如果当前是超级管理员登录，那么可以看到所有项目的已成交客户年龄段比例
            if (roleId.equals(Constants.ADMIN_ROLE)) {
                list = fnglCustomerMapper.getTurnOverCustomer("", statisWhere);
            }
            page.setRows(list);
            page.setTotal(new PageInfo(list).getTotal());
            return ResultGenerator.genSuccessResult(page);
        }
        return ResultGenerator.genFailResult("查询失败");
    }

    private void initStatistics(StatisWhere where) {
        StringBuffer columns = new StringBuffer("cus.project_id as projectId,pr.name as projectName");
        StringBuffer groups = new StringBuffer("cus.project_id,cus.name");
        try{
            if(StringUtils.isNotBlank(where.getItems())){
                //支付方式转义
                if(where.getItems().contains("payment_method")){
                    columns.append(",(select name from sys_dict ct where ct.value=cus.payment_method and ct.type='paytype') as paymentMethod");
                }
                if(where.getItems().contains("occupation")){
                    columns.append(",(select name from sys_dict ct where ct.value=cus.occupation and ct.type='occupation') as occupationName");
                }
                if(where.getItems().contains("province")){
                    columns.append(",(select name from sys_dict ct where ct.value=cus.province and ct.type='province') as provinceName");
                }
                if(where.getItems().contains("area")){
                    columns.append(",(select name from sys_dict ct where ct.value=cus.area and ct.type='area') as areaName");
                }
                if(where.getItems().contains("city")){
                    columns.append(",(select name from sys_dict ct where ct.value=cus.city and ct.type='city') as cityName");
                }
                columns.append(","+where.getItems());
                groups.append(","+where.getItems());
            }
            columns.append(",count(1) as num");
        }catch (Exception e){
            e.printStackTrace();
        }
        where.setColumnItem(columns.toString());
        where.setGroupItme(groups.toString());

    }

    /**
     * 每日录入客户分析
     */
    @Override
    public Result getEverydayCustomer(Page page,StatisWhere statisWhere) {

        if (page.getLimit() != null && page.getPage() != 0) {

            // 获取到当前登录用户所属角色
            User user = CommonUtil.getUser();
            LrdUserrole userrole = userRoleServiceImpl.findBy("userId", user.getUser_id());
            String roleId = userrole.getRoleId();
            // 根据用户手机号获取到人员信息
            FnglStaff fnglStaff = fnglStaffService.findBy("phone", user.getUser_id());
            List<FnglCustomer> list = new ArrayList<>();
            PageHelper.startPage(page.getPage(), page.getLimit(), true);
            //初始化统计sql
            initStatistics1(statisWhere);
            // 如果当前是销售经理登录，那么只能看到自己所属项目的已成交客户年龄段比例
            if (roleId.equals(Constants.MANAGER_ROLE)) {
                list = fnglCustomerMapper.getEverydayCustomer(fnglStaff.getProjectId(), statisWhere);
            }
            // 如果当前是超级管理员登录，那么可以看到所有项目的已成交客户年龄段比例
            if (roleId.equals(Constants.ADMIN_ROLE)) {
                list = fnglCustomerMapper.getEverydayCustomer("", statisWhere);
            }
            page.setRows(list);
            page.setTotal(new PageInfo(list).getTotal());
            return ResultGenerator.genSuccessResult(page);
        }
        return ResultGenerator.genFailResult("查询失败");
    }

    private void initStatistics1(StatisWhere where) {
        StringBuffer columns = new StringBuffer("cus.project_id as projectId,pr.name as projectName");
        StringBuffer groups = new StringBuffer("cus.project_id");
        try{
                if(where.getUnit().contains("year")){
                    columns.append(",DATE_FORMAT(cus.create_time,'%Y') as createTime");
                    groups.append(",DATE_FORMAT(cus.create_time,'%Y')");
                }
                if(where.getUnit().contains("month")){
                    columns.append(",DATE_FORMAT(cus.create_time,'%Y-%m') as createTime");
                    groups.append(",DATE_FORMAT(cus.create_time,'%Y-%m')");
                }
                if(where.getUnit().contains("day")){
                    columns.append(",DATE_FORMAT(cus.create_time,'%Y-%m-%d') as createTime");
                    groups.append(",DATE_FORMAT(cus.create_time,'%Y-%m-%d')");
                }
                if(where.getItems().contains("staffId")){
                    columns.append(",(select real_name from fngl_staff st where st.staff_id = cus.staff_id) as staffName");
                    groups.append(",cus.staff_id");
                }
            columns.append(",count(1) as num");
        }catch (Exception e){
            e.printStackTrace();
        }
        where.setColumnItem(columns.toString());
        where.setGroupItme(groups.toString());

    }

    /**
     *  获取指定数据报表
     */
    @Override
    public List<FnglCustomer> getFnglCustomerUser() {

        List<FnglCustomer> list = fnglCustomerMapper.getFnglCustomerUser();

        return list;
    }

    @Override
    public Result exportFnglCustomer(FnglCustomer fnglCustomer) {
        Condition condition = new Condition(FnglCustomer.class);
        Criteria criteria=condition.createCriteria();
        initCriteria(criteria,fnglCustomer);
        condition.setOrderByClause("create_time desc");
        List<FnglCustomer> result = this.findByCondition(condition);
        return ResultGenerator.genSuccessResult(result);
    }
}
